﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

namespace EWDTAssignment
{
    public partial class ManageStaff : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            GridView1.DataSourceID = "ObjectDatasource1";
            GridView1.DataBind();
            
        }

        protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
        {
            Label lblfname = (Label)GridView1.SelectedRow.FindControl("Label2");
            txtbxFName.Text = lblfname.Text;
            Label lbllname = (Label)GridView1.SelectedRow.FindControl("Label3");
            txtbxLname.Text = lbllname.Text;
            Label lblgender = (Label)GridView1.SelectedRow.FindControl("lblgender");
            txtbxGender.Text = lblgender.Text;
            Label lbladdress = (Label)GridView1.SelectedRow.FindControl("Label6");
            txtbxAddress.Text = lbladdress.Text;
            Label lblcon = (Label)GridView1.SelectedRow.FindControl("Label4");
            txtbxContactNum.Text = lblcon.Text;
            Label lblemail = (Label)GridView1.SelectedRow.FindControl("Label5");
            txtbxEmail.Text = lblemail.Text;
            Label lbldob = (Label)GridView1.SelectedRow.FindControl("lbldob");
            txtbxDob.Text = Convert.ToDateTime(lbldob.Text).ToShortDateString();
            Label lblQuestion = (Label)GridView1.SelectedRow.FindControl("lblQuestion");
            txtbxQuestion.Text = lblQuestion.Text;
            Label lblAnswer = (Label)GridView1.SelectedRow.FindControl("lblAnswer");
            txtbxAnswer.Text = lblAnswer.Text;
            Label lblUserName = (Label)GridView1.SelectedRow.FindControl("lblUserName");
            txtbxUserName.Text = lblUserName.Text;
            Label lblpostal = (Label)GridView1.SelectedRow.FindControl("lblpostal");
            txtbxPostalC.Text = Convert.ToInt32(lblpostal.Text).ToString();
            Label lblAccountId = (Label)GridView1.SelectedRow.FindControl("label1");
            txtbxAccountId.Text = Convert.ToInt32(lblAccountId.Text).ToString();
            Label lblAccountType = (Label)GridView1.SelectedRow.FindControl("lblAccountType");
            txtbxAccountId.Text = Convert.ToInt32(lblAccountType.Text).ToString();


         
        }

        protected void BtnViewStaff_Click(object sender, EventArgs e)
        {
            GridView1.DataSourceID = "viewStaff";
           GridView1.DataBind();
        }

        protected void BtnViewAll_Click(object sender, EventArgs e)
        {

        }

        protected void LinkButton1_Click(object sender, EventArgs e)
        {

        }

        protected void DisplayAlert(string message)
        {
            ClientScript.RegisterStartupScript(
            this.GetType(),
            Guid.NewGuid().ToString(),
            string.Format("alert('{0}');",
            message.Replace("'", @"\'").Replace("\n", "\\n").Replace("\r", "\\r")),
            true);
        }

        protected void BtnViewCustomer_Click(object sender, EventArgs e)
        {
            GridView1.DataSourceID = "viewCust";
            GridView1.DataBind();
        }

        protected void btnDel_Click(object sender, EventArgs e)
        {
            int accountid = Convert.ToInt32(txtbxAccountId.Text.ToString());
            App_Code.DBManager.DelSetItem(accountid);
            App_Code.DBManager.DelShoppingCart(accountid);
            App_Code.DBManager.DelAccount(accountid);
            DisplayAlert("Account Deleted");
                
            }

        protected void BtnCreateNew_Click(object sender, EventArgs e)
        {
            txtbxFName.Text = string.Empty;
            txtbxAccountType.Text = string.Empty;
            txtbxAccountType.Enabled = true;
            txtbxLname.Text = string.Empty;
            txtbxAddress.Text = string.Empty;
            txtbxAnswer.Text = string.Empty;
            txtbxQuestion.Text = string.Empty;
            txtbxUserName.Text = string.Empty;
            txtbxPostalC.Text = string.Empty;
            txtbxEmail.Text = string.Empty;
            txtbxContactNum.Text = string.Empty;
            txtbxDob.Text = string.Empty;
            txtbxAccountId.Text = string.Empty;
            txtbxGender.Text = string.Empty;
               
        }

        protected void btnSumit0_Click(object sender, EventArgs e)
        {
            
            int accountid = Convert.ToInt32(txtbxAccountId.Text.ToString());
            string accountType = txtbxAccountType.Text;
            string firstname = txtbxFName.Text;
            string lastName = txtbxLname.Text;
            string userName = txtbxUserName.Text;
            string password = txtbxPassword.Text;
            string question = txtbxQuestion.Text;
            string answer = txtbxAnswer.Text;
            int contactNum = Convert.ToInt32(txtbxContactNum.Text.ToString());
            string email = txtbxEmail.Text.ToString();
            int postalCode = Convert.ToInt32(txtbxPostalC.Text.ToString());
            DateTime DOB = Convert.ToDateTime(txtbxDob.Text.ToString());
            string address = txtbxAddress.Text;
            string gender = txtbxGender.Text;

            if (txtbxAccountId.Text == string.Empty)
            {

                App_Code.DBManager.InsertNewAccount(accountid, accountType, userName, password, question, answer, firstname, lastName, contactNum, address, email, postalCode, DOB, gender);
                DisplayAlert("Account Added");
               
            }
            else
            {
                
                App_Code.DBManager.EditAccount(accountid, accountType, userName, password, question, answer, firstname, lastName, contactNum, address, email, postalCode, DOB, gender);
                DisplayAlert("Account Updated");
            }
        }
        }
 
    }
